package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.tour.pojo.HzyTravelers;
import com.bdqn.tour.service.impl.HzyTravelersServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getTraveler")
public class GetTravelerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        // 获取搜索参数，如果为null则设置为空字符串
        String searchName = req.getParameter("search");
        searchName = (searchName == null) ? "" : searchName.trim();


        // 获取旅客列表
        HzyTravelersServiceImpl service = new HzyTravelersServiceImpl();
        List<HzyTravelers> travelersList = service.getTravelersList(searchName);

        // 转换为JSON并返回
        String json = JSONObject.toJSONString(travelersList);

        resp.getWriter().write(json);
    }
}
